package HighConcurrency.Nio;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class bufferDemo {
    public static void main(String[] args) {
        ByteBuffer buffer= ByteBuffer.allocate(128);//构建
        System.out.println("长度"+buffer.capacity());//输出容量位
        buffer.put("abc".getBytes());//赋值
        buffer.put("def".getBytes());

        // buffer.position(0);//操作位
        byte b=buffer.get();//获取
        System.out.println(b);

        //buffer.limit(buffer.position());
        //buffer.position(0);
        buffer.flip();//等于上面两个

        //while (buffer.position()<buffer.limit())
        while (buffer.hasRemaining())//等价于上面那个
        {
            b=buffer.get();
            System.out.println(buffer.position()+"."+(char)b);
        }


        //数据已知，构建缓冲区
        ByteBuffer buffer1=ByteBuffer.wrap("styuwmlye".getBytes());
        //未进行get/put，position（操作位）默认在0位




    }
}
